package com.blocks.mq.listener.receiver;


import com.alibaba.fastjson.JSONObject;
import com.blocks.mq.listener.entity.Student;
import com.blocks.mq.listener.example.service.ExampleTxtWriterService;
import com.blocks.mq.listener.example.service.ExampleXlsxWriterService;
import com.blocks.mq.listener.repository.StudentRepository2;
import com.zut.blocks.base.mq.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: cjc
 * Date: 2020/12/1
 * Time: 10:24
 * Description: No Description
 */
@Component
public class QueueConsumerListener {

    Logger logger = LoggerFactory.getLogger(QueueConsumerListener.class);

    @Autowired
    StudentRepository2 studentRepository2;

    @Autowired
    ExampleXlsxWriterService exws;

    @Autowired
    ExampleTxtWriterService etws;

    //queue模式的消费者
    @JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "queueListener")
    public void readActiveQueue(String message) {

        logger.info("receive msg: " + message);


        Message messages = JSONObject.parseObject(message, Message.class);
        Message.MessageType type = messages.getType();
        if (type == Message.MessageType.str) {
            System.out.println(messages.getData());
            Student student = JSONObject.parseObject(messages.getData(), Student.class);

            Student s2 = studentRepository2.save(student);
        }


    }



}